package com.mojo.concurrent.concurrent_07_pool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 周期任务线程池
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2023, 广州九尾信息科技有限公司
 * @date 2023/2/20 10:50
 */
public class ScheduledThreadPoll01 {
    static ScheduledExecutorService scheduledExecutorService;

    static {
        scheduledExecutorService = Executors.newScheduledThreadPool(2);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            // scheduledExecutorService.schedule(new Runnable() {
            //     @Override
            //     public void run() {
            //         try {
            //             Thread.sleep(1000);
            //         } catch (InterruptedException e) {
            //             throw new RuntimeException(e);
            //         }
            //         System.out.println(finalI + "打印线程" + Thread.currentThread().getName());
            //     }
            // }, 1, TimeUnit.SECONDS);
            // scheduledExecutorService.scheduleWithFixedDelay()
        }
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("<<<" + System.currentTimeMillis());
            // initialDelay:初始延迟，第一次任务与initialDelay后执行
            // period：间隔时间
        }, 10L, 3L, TimeUnit.SECONDS);
    }
}
